package com.pachong.frameworkdemo.module2;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.pachong.android.baseuicomponent.CompState;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.framework.citypicker.ChooseCityDialogFragment;
import com.pachong.android.framework.citypicker.Province;
import com.pachong.android.framework.datetimepicker.DatePickerFragment;
import com.pachong.android.frameworkbase.customviews.ppw.CommonPopupWindow;
import com.pachong.android.frameworkbase.utils.EasyToast;
import com.pachong.baidumaplib.location.city.CityChoiceActivity;
import com.pachong.frameworkdemo.R;
import com.pachong.frameworkdemo.demo.qrcode.QrCodeDemoActivity;
import com.pachong.frameworkdemo.module1.EditTextDemo;
import com.pachong.frameworkdemo.module1.MultiDemoActivity;
import com.pachong.frameworkdemo.module1.PictureBrowDemoActivity;
import com.pachong.frameworkdemo.module1.PicturePickerDemoActivity;

import java.util.ArrayList;
import java.util.Calendar;

import butterknife.Bind;
import butterknife.OnClick;

public class Main2Fragment extends BaseFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        setState(CompState.DATA);
        return view;

    }


    @Override
    public View createDataView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        return layoutInflater.inflate(R.layout.fragment_main2, null);
    }

    @OnClick(R.id.btnSelectDate)
    public void onClick() {
        final DatePickerFragment frag = new DatePickerFragment(Calendar.getInstance());
//        frag.setCurrentDate(Calendar.getInstance());
        frag.setOnDateSelectedListener(new DatePickerFragment.OnDateSelectedListener() {
            @Override
            public void onDateSelected(int year, int monthOfYear, int dayOfMonth) {
                Log.e("vivi", "year == " + year + " month of year =  " + monthOfYear + "  dayOf month == " + dayOfMonth);
                frag.dismiss();
            }
        });
        frag.show(getActivity().getSupportFragmentManager());
    }

    @OnClick(R.id.btnSelectArea)
    public void onBtnSelectAreaClick() {
        ChooseCityDialogFragment chooseCityDialog = new ChooseCityDialogFragment();
        chooseCityDialog.show(getChildFragmentManager(), "tag");
        chooseCityDialog.setOnCitySelectedListener(new ChooseCityDialogFragment.OnCitySelectedListener() {

            @Override
            public void onSelected(Province province, Province.City city, Province.Country country) {
                String str = "province = " + province.name + " " + province.id +
                "city = " + city.name + " " + city.id +
                        " country = " + country.name + " " + country.id;
                EasyToast.showToast(getActivity(), "str == " + str);
            }
        });
    }


    @OnClick(R.id.btnPicBrowserDemo)
    public void onShowPicBrowserDemo() {
        PictureBrowDemoActivity.newInstance(getActivity());
//        ScDemo.newInstance(getActivity());
    }

    @OnClick(R.id.btnPickerDemo)
    public void onShowPicPickerDemo() {
        Intent intent = new Intent(getActivity(), PicturePickerDemoActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.btnCityChoice)
    public void onShowCityChoseDemo() {
        Intent intent = new Intent(getActivity(), CityChoiceActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.btnMutiViewpager)
    public void onShowViewpagerDemo() {
        MultiDemoActivity.newInstance(getActivity());
    }

    @OnClick(R.id.btn_clip_image)
    public void onShowClipImageDemo() {
//        PhotoActionHelper.clipImage(getActivity())
//                .input("/storage/emulated/0/aaaaa.png")
//                .requestCode(/** 传入请求码*/3303)
//                .output(Environment.getExternalStorageDirectory().getAbsolutePath()+"/aaaatest.jpg").start();
    }

    @OnClick(R.id.btnEditDemo)
    public void onShowEditTextDemo() {
        EditTextDemo.newInstance(getActivity());
    }


    @Bind(R.id.btnPpw)
    Button btnPpw;

    @OnClick(R.id.btnPpw)
    public void onBtnPpwClicked() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.popup_window_content, null);
        CommonPopupWindow popupWindow = new CommonPopupWindow(view);
        int xOff = 100;
        int yOff = 20;
        popupWindow.showAsDropDown(btnPpw, xOff, yOff);
    }

    @Bind(R.id.btnQRCode)
    Button btnQRCode;

    @OnClick(R.id.btnQRCode)
    public void onBtnQRCode() {
        Intent intent = new Intent(getActivity(), QrCodeDemoActivity.class);
        startActivity(intent);
    }


    @Bind(R.id.btnListPpw1)
    Button btnListPpw1;

    @OnClick(R.id.btnListPpw1)
    public void onBtnListPpw1() {
        final ArrayList<String> mArrayList = new ArrayList<String>();
        mArrayList.add("第一ddddddddd");
        mArrayList.add("第二个子项");
        mArrayList.add("第三个子项");
/*
        ListPopupWindow1 mListPopupWindow = new ListPopupWindow1(getContext());
//        mListPopupWindow=new ListPopupWindow1(mContext, null, android.support.v7.appcompat.R.attr.listPopupWindowStyle, R.style.TestListPop);

        mListPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#cccccc")));
        //自定义Adapter
        ListPopupWindowAdapter mListPopupWindowAdapter = new ListPopupWindowAdapter(mArrayList, getContext());
        mListPopupWindow.setAdapter(mListPopupWindowAdapter);
        //mListPopupWindow.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.ic_launcher));
        mListPopupWindow.setWidthMatchParent(true);
        mListPopupWindow.setWidth(ListPopupWindow.MATCH_PARENT);
        mListPopupWindow.setHeight(ListPopupWindow.WRAP_CONTENT);
        mListPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                Toast.makeText(getContext(), "点击了" + mArrayList.get(position), Toast.LENGTH_SHORT).show();
            }
        });
        mListPopupWindow.setAnchorView(btnListPpw1);
        mListPopupWindow.show();*/

    }


    public class ListPopupWindowAdapter extends BaseAdapter {
        private ArrayList<String> mArrayList;
        private Context mContext;

        public ListPopupWindowAdapter(ArrayList<String> list, Context context) {
            super();
            this.mArrayList = list;
            this.mContext = context;
        }

        @Override
        public int getCount() {
            if (mArrayList == null) {
                return 0;
            } else {
                return this.mArrayList.size();
            }
        }

        @Override
        public Object getItem(int position) {
            if (mArrayList == null) {
                return null;
            } else {
                return this.mArrayList.get(position);
            }
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem_popup_window_demo, parent, false);
                holder.itemTextView = (TextView) convertView.findViewById(R.id.itemTextView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            if (this.mArrayList != null) {
                final String itemName = this.mArrayList.get(position);
                if (holder.itemTextView != null) {
                    holder.itemTextView.setText(itemName);
                }
            }

            return convertView;

        }

        private class ViewHolder {
            TextView itemTextView;
        }

    }


}
